"""
Case Type   : 资源池化集群支持多链路
Case Name   : failover过程中断开升主节点全链路
Create At   : 2025/04/13
Owner       : @kyleze
Description :
    1.查询集群状态
    2.构建failover
    3.failover过程中断开升主节点全链路
    4.等待failover完成
    5.恢复链路
    6.恢复集群
Expect      :
    1.数据库状态正常
    2.成功
    3.断开成功
    4.failvoer成功，集群恢复正常
    5.成功
    6.成功
History     :
"""
import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread

from yat.test import Node
from yat.test import macro


class MultipathTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pri_user_node = Node('PrimaryDbUser')
        self.pri_root = Node(node="PrimaryRoot")
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.sta1_root = Node(node="Standby1Root")
        self.sta1_user_node = Node('Standby1DbUser')
        self.sta2_sh = CommonSH('Standby2DbUser')
        self.sta2_root = Node(node="Standby2Root")
        self.sta2_user_node = Node('Standby2DbUser')
        self.ip_1 = macro.STANDBY1_LUN_IP.split()[0].strip()
        self.ip_2 = macro.STANDBY1_LUN_IP.split()[1].strip()
        self.ip_3 = macro.STANDBY2_LUN_IP.split()[0].strip()
        self.ip_4 = macro.STANDBY2_LUN_IP.split()[1].strip()
        self.log.info("----获取备1节点网络设备名称----")
        self.st1_network_card = []
        interfaces = self.sta1_root.sh("ip -o addr show").result()
        for item in interfaces.split('\n'):
            if self.ip_1 in item or self.ip_2 in item:
                self.st1_network_card.append(item.split(' ')[1].split(':')[0])
        self.log.info(self.st1_network_card)
        self.log.info("----获取备2节点网络设备名称----")
        self.st2_network_card = []
        interfaces = self.sta2_root.sh("ip -o addr show").result()
        for item in interfaces.split('\n'):
            if self.ip_3 in item or self.ip_4 in item:
                self.st2_network_card.append(item.split(' ')[1].split(':')[0])
        self.log.info(self.st2_network_card)

    def test_multipath(self):
        step = '--step1:查询集群状态 expect:成功--'
        self.log.info(step)
        cluster_status = self.primary_sh.get_db_cluster_status('status')
        self.log.info(cluster_status)
        self.assertTrue(cluster_status, "执行失败" + step)

        step = '--step2:构建failover expect:成功--'
        self.log.info(step)
        mv_cmd = f"mv {macro.GAUSS_HOME}/bin/gaussdb " \
                 f"{macro.GAUSS_HOME}/bin/gaussdb_bak"
        self.log.info(mv_cmd)
        move_res = self.pri_user_node.sh(mv_cmd).result()
        self.log.info(move_res)
        self.assertIn('', move_res, "执行失败" + step)
        kill_cmd = "ps ux | grep '\-D' | grep gaussdb| grep -v grep " \
                   "| awk '{{print $2}}' | xargs kill -9"
        self.log.info(kill_cmd)
        kill_res = self.pri_user_node.sh(kill_cmd).result()
        self.log.info(kill_res)
        self.assertIn('', kill_res, "执行失败" + step)

        step = '--step3:failover过程中断开升主节点全链路 expect:成功--'
        self.log.info(step)
        for i in range(30):
            time.sleep(1)
            res = self.primary_sh.get_dms_reformer_lock()
            self.log.info(res)
            if res == '6002':
                self.new_pri_nodeid = 6002
                res = self.sta1_root.sh(
                    f'ifdown {self.st1_network_card[0]};'
                    f'ifdown {self.st1_network_card[1]}').result()
                self.log.info(res)
                self.assertEqual(
                    2, res.count("successfully deactivated"), "执行失败" + step)
                break
            elif res == '6003':
                self.new_pri_nodeid = 6003
                res = self.sta2_root.sh(
                    f'ifdown {self.st2_network_card[0]};'
                    f'ifdown {self.st2_network_card[1]}').result()
                self.log.info(res)
                self.assertEqual(
                    2, res.count("successfully deactivated"), "执行失败" + step)
                break

        step = '--step4:等待failover完成 expect:成功--'
        self.log.info(step)
        for i in range(60):
            self.log.info("----第" + str(i + 1) + "次查询集群状态----")
            time.sleep(2)
            cluster_detail = self.primary_sh.get_db_cluster_status("detail")
            self.log.info(cluster_detail)
            if "Degraded" in cluster_detail and "Primary Normal" in \
                    cluster_detail:
                break
            elif i == 59:
                cluster_res = "Degraded" or "Primary Normal" in cluster_detail
                self.assertTrue(cluster_res, "执行失败" + step)

        step = '--step5:恢复链路 expect:成功--'
        self.log.info(step)
        if self.new_pri_nodeid == 6002:
            res = self.sta1_root.sh(f'ifup {self.st1_network_card[0]};'
                                    f'ifup {self.st1_network_card[1]}').result()
            self.log.info(res)
            self.assertEqual(
                2, res.count("successfully activated"), "执行失败" + step)
        else:
            res = self.sta2_root.sh(f'ifup {self.st2_network_card[0]};'
                                    f'ifup {self.st2_network_card[1]}').result()
            self.log.info(res)
            self.assertEqual(
                2, res.count("successfully activated"), "执行失败" + step)

        step = '--step6:恢复集群 expect:成功--'
        self.log.info(step)
        mv_cmd = f"mv {macro.GAUSS_HOME}/bin/gaussdb_bak " \
                 f"{macro.GAUSS_HOME}/bin/gaussdb"
        move_res = self.pri_user_node.sh(mv_cmd).result()
        self.log.info(move_res)
        self.assertIn('', move_res, "执行失败" + step)
        start_res = self.primary_sh.exec_cm_ctl("start")
        self.log.info(start_res)
        self.assertIn(self.constant.cm_start_success_msg, start_res,
                      '执行失败' + step)
        start_res = self.primary_sh.exec_cm_ctl("switchover", '-a')
        self.log.info(start_res)
        self.assertIn(self.constant.cm_switchover_success_msg, start_res,
                      '执行失败' + step)
        cluster_status = self.primary_sh.get_db_cluster_status('status')
        self.log.info(cluster_status)
        self.assertTrue(cluster_status, "执行失败" + step)

    def tearDown(self):
        self.log.info(f'----{os.path.basename(__file__)} end----')

